#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:Vergil
# datetime:2018/9/22 20:31
# software: PyCharm
import logging
import sys
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


current_page = 1

logger = logging.getLogger('main.p_collect')


def course_choose(driver, course_name):
    try:
        courseUrl = driver.find_element_by_xpath("//a[contains(@title,'%s')]" % course_name).get_attribute(
            'data-2')
        driver.get('https://spoc.tfswufe.edu.cn' + courseUrl)

        nextUrl = driver.find_element_by_xpath(
            "//a[text()='课程讨论区']").get_attribute('href')

        driver.get(nextUrl)

        logger.info('已成功进入课程讨论区，请继续')

    except Exception:
        logger.info('------未找到该课程，请检查输入是否正确------')
        name = input('重新输入课程名')
        course_choose(driver, name)


def get_url_lst(driver, browse_times):
    post_url_lst = []
    i = 1
    logger.info('----总共刷%s次----' % str(browse_times))

    while i < int(browse_times):
        try:
            wait = WebDriverWait(driver, 10)
            wait.until(
                EC.presence_of_element_located(
                    (By.XPATH, '//div[@class=" panel-body"]/table/thead')))
            post_elements = driver.find_element_by_tag_name(
                'tbody').find_elements_by_tag_name('tr')
            li_page = driver.find_elements_by_xpath(
                "//div[@id='page2']/ul/li")[-1].get_attribute('data-lp')

            for post_element in post_elements:
                try:
                    post_url = post_element.find_element_by_tag_name(
                        'a').get_attribute('href')
                    post_url_lst.append(post_url)
                except Exception as e:
                    logger.exception(e)

            page = page_turn(driver)
            next_page = int(page) + 1
            try:
                while page == li_page:
                    logger.info('-------已是最后一页-------')
                    logger.info('------URL抓取完成------')
                    return post_url_lst

                if int(page) % 10 == 0:
                    driver.find_element_by_class_name(
                        'next').find_element_by_tag_name('a').click()
                    logger.info('------即将点击第%s页------' % next_page)
                else:
                    wait.until(
                        EC.presence_of_element_located(
                            (By.ID, 'page2')))
                    driver.find_element_by_xpath(
                        "//ul/li/a[text()=%s]" %
                        str(next_page)).click()
                    logger.info('------即将点击第%s页------' % next_page)

            except Exception as e:
                logger.info('翻页失败')
            time.sleep(2)
            i = len(post_url_lst)

        except TypeError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

    return post_url_lst


def page_turn(driver):
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'page2'))
        )

        current_page = element.find_element_by_xpath('//ul/li[@class="disabled"]')
        if current_page.text != '':
            return current_page.text

    except Exception as e:
        logger.exception(e)


def main(driver, course_name, browse_times):

    course_choose(driver, course_name)
    url_lst = get_url_lst(driver, browse_times)

    return url_lst
